package top.hzy520.attend.controller;

import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.hzy520.attend.dto.AttendDto;
import top.hzy520.attend.dto.AttendSearchInputDto;
import top.hzy520.attend.entity.AttendItem;
import top.hzy520.attend.entity.AttendPage;
import top.hzy520.attend.service.AttendService;
import top.hzy520.common.constants.Constants;
import top.hzy520.common.resp.ApiResult;
import top.hzy520.user.entity.User;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 14:45 2018/8/7
 */
@Controller
public class AttendController {
    SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");

    @Autowired
    @Qualifier("attendServiceImpl")
    private AttendService attendService;
     @RequestMapping("/attendrecord")
    public String attendLsit(){
        return "admin/attend/attendlist";
    }
    @RequestMapping("/attend/list")
    @ResponseBody
    public AttendPage attendList(int limit, int offset, HttpServletRequest request){
      User user= (User) request.getSession().getAttribute("user");
      List<AttendItem> attendItemList=attendService.selectAttendList(user.getId(),offset,limit);
      int count=attendService.selectAttendAcount(user.getId());
        AttendPage attendPage=new AttendPage();
        attendPage.setRows(attendItemList);
        attendPage.setTotal(count);
        return attendPage;
    }
    @RequestMapping("/attend/search")
    @ResponseBody
    public AttendPage attendSearch(AttendSearchInputDto searchInputDto,HttpServletRequest request) throws ParseException {
        AttendDto attendDto=new AttendDto();
        attendDto.setOffset(searchInputDto.getOffset());
        attendDto.setLimit(searchInputDto.getLimit());
         if (!"".equals(searchInputDto.getStartDate())){
             attendDto.setStartDate(format.parse(searchInputDto.getStartDate()));
         }
        if (!"".equals(searchInputDto.getEndDate())){
             attendDto.setEndDate(format.parse(searchInputDto.getEndDate()));
        }
        attendDto.setAttendStatus(searchInputDto.getAttendStatus());
       User user= (User) request.getSession().getAttribute("user");
       attendDto.setUserId(user.getId());
      List<AttendItem> attendItemList=attendService.selectBySearch(attendDto);
      int count=attendService.selectAttendAcountBySearch(attendDto);
      AttendPage attendPage=new AttendPage();
      attendPage.setRows(attendItemList);
      attendPage.setTotal(count);
      return attendPage;
    }
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    @PostMapping("/attend")
    @ResponseBody
    public ApiResult attend(String attendDate) throws ParseException {
      Date date=  df.parse(attendDate);
      boolean result=  DateUtils.isSameDay(date, new Date());
        ApiResult apiResult=new ApiResult(Constants.RESP_STATUS_OK,"打卡成功");
        return apiResult;
    }
}
